package com.ourteam.workspace.api;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import com.ourteam.product.ui.dao.ProductUIPageParamDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIPageBean;
import com.ourteam.product.ui.domain.ProductUIPageParamBean;
import com.ourteam.product.ui.domain.ProductUIPageTemplateBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

public class ProductUICodeGenerator {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	public static String generateProductUIPagetCode(VelocityEngine velocityEngine, ProductUIPageBean uiPageBean,
			Map<String, Object> vmContenxt) throws Exception {

		Map<String, Object> contenxt = new HashMap<String, Object>();

		if (vmContenxt != null && vmContenxt.isEmpty() == false) {
			contenxt.putAll(vmContenxt);
		}

		contenxt.put("strUtils", StringUtils.class);

		contenxt.put("uiConfig", new UIConfgiUtils());

		contenxt.put("generateDate", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

		ProductUIPageTemplateBean templateBean = PRODUCT_UI_SERVICE
				.getProductUIPageTemplateById(uiPageBean.getPageTemplateId());

		ProductUIPageParamDAOQueryBean pageParamQueryBean = new ProductUIPageParamDAOQueryBean();

		pageParamQueryBean.createCriteria().andPageIdEqualTo(uiPageBean.getId());

		ProductUIPageParamBean[] paramBeans = PRODUCT_UI_SERVICE.queryProductUIPageParams(pageParamQueryBean);

		Map<String, String> pageParamMap = new HashMap<>();

		for (int i = 0; i < paramBeans.length; i++) {
			if (StringUtils.startsWith(paramBeans[i].getParamInputType(), "UI")
					&& paramBeans[i].getPageParamValueId() != 0) {

				if ("UIActionGroup".equals(paramBeans[i].getParamInputType())) {
					pageParamMap.put(paramBeans[i].getParamCode(), "ProductUI:" + PRODUCT_UI_SERVICE
							.getProductUIActionGroupById(paramBeans[i].getPageParamValueId()).getActionGroupCode());
				} else if ("UIList".equals(paramBeans[i].getParamInputType())) {
					pageParamMap.put(paramBeans[i].getParamCode(),
							"ProductUI:" + PRODUCT_UI_SERVICE.getProductUIListById(paramBeans[i].getPageParamValueId()).getCode());
				} else if ("UIForm".equals(paramBeans[i].getParamInputType())) {
					pageParamMap.put(paramBeans[i].getParamCode(),
							"ProductUI:" + PRODUCT_UI_SERVICE.getProductUIFormById(paramBeans[i].getPageParamValueId()).getCode());
				} else if ("UITable".equals(paramBeans[i].getParamInputType())) {
					pageParamMap.put(paramBeans[i].getParamCode(),
							"ProductUI:" + PRODUCT_UI_SERVICE.getProductUITableById(paramBeans[i].getPageParamValueId()).getCode());
				} else if ("UITree".equals(paramBeans[i].getParamInputType())) {
					pageParamMap.put(paramBeans[i].getParamCode(),
							"ProductUI:" + PRODUCT_UI_SERVICE.getProductUITreeById(paramBeans[i].getPageParamValueId()).getCode());
				} else if ("UITab".equals(paramBeans[i].getParamInputType())) {
					pageParamMap.put(paramBeans[i].getParamCode(),
							"ProductUI:" + PRODUCT_UI_SERVICE.getProductUITabById(paramBeans[i].getPageParamValueId()).getCode());
				} else if ("UIPage".equals(paramBeans[i].getParamInputType())) {
					pageParamMap.put(paramBeans[i].getParamCode(),
							PRODUCT_UI_SERVICE.getProductUIPageById(paramBeans[i].getPageParamValueId()).getPageCode());
				}

			} else {
				pageParamMap.put(paramBeans[i].getParamCode(), paramBeans[i].getPageParamValue());
			}

		}

		contenxt.put("page", pageParamMap);

		StringWriter codeWriter = new StringWriter();

		velocityEngine.evaluate(new VelocityContext(contenxt), codeWriter, "code Template",
				new StringReader(new String(Base64.decodeBase64(templateBean.getTemplateContent()), "UTF-8")));

		return codeWriter.toString();
	}
}
